var prefix = ctx + "module/workFlow"

$(function() {
	var columns = [{
            checkbox: true
        },
				{
			field : 'flowId', 
			title : '主键' 
		},
				{
			field : 'flowName', 
			title : '工作流名称' 
		},		
				{
			field : 'flowCode', 
			title : '工作流代码' 
		},
				{
            field: 'flowState',
            title: '状态',
            formatter: function(value, row, index) {
                if (value == 0) {
                    return '<span class="label label-success">正常</span>';
                } else if (value == 1) {
                    return '<span class="label label-danger">暂停</span>';
                }
            }
        },
				{
			field : 'remark', 
			title : '备注' 
		},
		        {
            title: '操作',
            align: 'center',
            formatter: function(value, row, index) {
            	var actions = [];
            	actions.push(statusTools(row));
				actions.push('<a class="btn btn-primary btn-sm ' + editFlag + '" href="#" title="编辑" mce_href="#" onclick="edit(\'' + row.flowId + '\')"><i class="fa fa-edit"></i></a> ');
				actions.push('<a class="btn btn-warning btn-sm ' + removeFlag + '" href="#" title="删除" onclick="remove(\'' + row.flowId + '\')"><i class="fa fa-remove"></i></a>');
				return actions.join('');
            }
        }];
	var url = prefix + "/list";
	$.initTable(columns, url);
});

function statusTools(row) {
    if (row.flowState == 1) {
        return '<a class="btn btn-primary btn-sm ' + statusFlag + '" href="#" title="启用" onclick="start(this,\'' + row.flowId + '\')"><i class="fa fa-check"></i></a> ';
    } else {
    	return '<a class="btn btn-primary btn-sm ' + statusFlag + '" href="#" title="停用" onclick="stop(this,\'' + row.flowId + '\')"><i class="fa fa-minus"></i></a> ';
    }
}

/*工作流-停用*/
function stop(obj, flowId) {
	$.modalConfirm("确认要停用吗？", function() {
		_ajax(prefix + "/save/", { "flowId": flowId, "flowState": 1 }, "post");
    })
}

/*工作流-启用*/
function start(obj, flowId) {
	$.modalConfirm("确认要启用吗？", function() {
		_ajax(prefix + "/save/", { "flowId": flowId, "flowState": 0 }, "post");
    })
}

/*工作流-新增*/
function add() {
    var url = prefix + '/add';
    layer_showAuto("新增工作流", url);
}

/*工作流-修改*/
function edit(flowId) {
    var url = prefix + '/edit/' + flowId;
    layer_showAuto("修改工作流", url);
}

// 单条删除
function remove(flowId) {
	$.modalConfirm("确定要删除选中工作流吗？", function() {
		_ajax(prefix + "/remove/" + flowId, "", "post");
    })
}

// 批量删除
function batchRemove() {
	var rows = $.getSelections("flowId");
	if (rows.length == 0) {
		$.modalMsg("请选择要删除的数据", "warning");
		return;
	}
	$.modalConfirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
		_ajax(prefix + '/batchRemove', { "ids": rows }, "post");
	});
}
